package com.gxuwz.medical.domain.chronicPay;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.policy.Policy;

/**
 * ���Բ���������ģ����
 * @author ̷��С
 *
 */
public class ChronicPay {
	
	private String payID; 
	private String persName; 
	private String cardId; 
	private String illName; 
	private Policy policy;  
	private double money;   
	private double payMoney; 
	private String year;  
	private String payTime; 
	public ChronicPay(){
		
	}
	public ChronicPay(String payID)throws SQLException{
		this.payID=payID;
		this.load(payID);
	}
	public String getPayID() {
		return payID;
	}
	public ChronicPay(String payID, String persName, String cardId,String illName,Policy policy, double money,double payMoney,String year,String payTime) {
		super();
		this.payID = payID;
		this.persName = persName;
		this.cardId = cardId;
		this.illName = illName;
		this.policy = policy;
		this.money = money;
		this.payMoney = payMoney;
		this.year = year;
		this.payTime = payTime;
	}
	public void load(String payID)throws SQLException{
		Connection conn=null;
		 PreparedStatement pstmt=null;
		 ResultSet rs=null;
		try{
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select * from chronicpay where payID=?");
			int index = 1;
			pstmt.setString(index, payID);
			rs=pstmt.executeQuery();
		 if(rs != null && rs.next()){
			 this.payID=rs.getString("payID");
			 this.persName =rs.getString("persName");
			 this.cardId =rs.getString("cardId");
			 this.illName =rs.getString("illName");
			 this.money =Double.parseDouble(rs.getString("money"));
			 this.payMoney =Double.parseDouble(rs.getString("payMoney"));
			 this.year =rs.getString("year");
			 this.payTime =rs.getString("payTime");
		 }
		}catch(SQLException e){
			throw new SQLException("ChronicPay with id " + payID
					+ " could not be loaded from the database.");
		}finally {
			DBUtil.close(rs, pstmt, conn);
		}
		
	}
	/**
	 *
	 * @throws SQLException
	 */
	public void add()throws SQLException{
		Connection conn=null;
       try{
    	    conn=DBUtil.getConn();
    	    conn.setAutoCommit(false);
    	 
    	    saveToDB(conn);
    	    conn.commit();
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	/**
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		// 转时间
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
			int i = (int) (Math.random() * 1000) + 1; 
			String payID = sf.format(new Date()) + i; 
		try{
			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into chronicpay");
			sqlBuff.append("(payID,persName,cardId,illName,money,payMoney,year,payTime)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?)");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			 //���ö�̬������Ӧ��ֵ
			 pstmt.setString(1, payID);
			 pstmt.setString(2, this.persName);
			 pstmt.setString(3, this.cardId);
			 pstmt.setString(4, this.illName);
			 pstmt.setDouble(5, this.money);
			 pstmt.setDouble(6, this.payMoney);
			 pstmt.setString(7, this.year);
			 pstmt.setString(8, this.payTime);
			 pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(pstmt);
			
		}
	}
	
/**
 * 
 * @param year
 * @param cardId
 * @return
 */
	public double countPayMoney(String year, String cardId) {
		this.year = year;
		this.cardId = cardId;
		Connection conn = null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		double sum = 0.0;		
		try {
			 conn = DBUtil.getConn();
			String sql = "SELECT SUM(payMoney) as sum FROM chronicPay  WHERE `year`=? and cardId=?";
			pstmt=conn.prepareStatement(sql);
		//设置值ֵ
			pstmt.setString(1, this.year);
			pstmt.setString(2, this.cardId);
			rs=pstmt.executeQuery();
			 if(rs.next()){
				sum = rs.getDouble("sum");
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sum;
					
	}
	
/**
 * 
 * @param payID
 * @throws SQLException
 */
	public void del(String payID)throws SQLException{
		Connection conn=null;
		this.payID=payID;
       try{
    	    conn=DBUtil.getConn();
			conn.setAutoCommit(false);
    	
    	    deleteFromDB(conn);
    	
			conn.commit();
       }catch(SQLException e){
    	   conn.rollback();
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
/**
 * 
 * @param conn
 * @throws SQLException
 */
	private void deleteFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			StringBuffer sqlBuff=new StringBuffer("delete from chronicpay where payID=?");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.payID);
			pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw new SQLException("Failed to delete record from table !", e);
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	public void setPayID(String payID) {
		this.payID = payID;
	}
	public String getPersName() {
		return persName;
	}
	public void setPersName(String persName) {
		this.persName = persName;
	}
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public String getIllName() {
		return illName;
	}
	public void setIllName(String illName) {
		this.illName = illName;
	}
	public double getPayMoney() {
		return payMoney;
	}
	public void setPayMoney(double payMoney) {
		this.payMoney = payMoney;
	}
	public String getPayTime() {
		return payTime;
	}
	public void setPayTime(String payTime) {
		this.payTime = payTime;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public Policy getPolicy() {
		return policy;
	}
	public void setPolicy(Policy policy) {
		this.policy = policy;
	}

}
